package com.zxq.dlt645;

import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin;
import com.zxq.dlt645.analy.AnalyseControlCode;
import com.zxq.dlt645.frame.DLT645Frame;
import com.zxq.factory.FastFrame;
import com.zxq.factory.encoder.impl.LongFieldEncoder;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DLT645AmmeterApplication implements CommandLineRunner {


    public static void main(String[] args) {
        SpringApplication.run(DLT645AmmeterApplication.class, args);
    }


    /**
     * 在发送帧信息之前，需要根据设备需要先发送前导码，前导码由0-4个FEH组成，所有数据项都是按照低字节在前，高字节在后的方式传输。通讯的时候现有主站发出信息帧，然后从站收到后根据信息做应答。
     * 发送：68 03 20 12 22 20 65 68 11 04 33 33 33 33 8D 16
     * 应答：68 03 20 12 22 20 65 68 91 08 33 33 33 33 33 33 33 33 DD 16
     * 发送：FE FE FE FE 68 18 20 12 22 20 65 68 11 04 33 32 34 35 A4 16
     * 应答：FE FE FE FE 68 18 20 12 22 20 65 68 91 0A 33 32 34 35 33 33 33 33 C7 56 13 16
     */
    @Override
    public void run(String... args) throws Exception {
        DLT645Frame dlt645Frame = new DLT645Frame();
        Long address = 3L
                + 32L * 256
                + 18L * 256 * 256
                + 34L * 256 * 256 * 256
                + 32L * 256 * 256 * 256 * 256
                + 101L * 256 * 256 * 256 * 256 * 256;
        dlt645Frame.setAddress(address);
        dlt645Frame.setCmd(0x91);
        System.out.println(AnalyseControlCode.analyseType((byte) 0x11));
        System.out.println(AnalyseControlCode.analyseCmd((byte) 0x11));
        byte[] data = new byte[]{0,0,0,0,0,0,0,0};
        dlt645Frame.setData(data);
        dlt645Frame.countLen();
        dlt645Frame.decorateEncoder();
        dlt645Frame.countCrc();
        byte[] total = FastFrame.encoder(dlt645Frame);
        System.out.println("frame:" + HexBin.encode(total));

    }

}
